package com.lanou.shunke.admin.controller;

import com.lanou.shunke.admin.entity.User;
import com.lanou.shunke.admin.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserAction {
    @Autowired
    private UserService service;

    private final Logger logger = LoggerFactory.getLogger(UserAction.class);

    @Value("${application.message:Hello World}")
    private String message;

    @GetMapping("/asd/{name}")
    public String welcome(@PathVariable String name, Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", this.message);
        return "welcome";
    }

    /*登陆*/
    @RequestMapping(value = "loginSuccess", method = {RequestMethod.POST})
    @ResponseBody
    public Boolean index(User user, HttpSession session) {
        Boolean set = service.login(user);
        if (set) {
            session.setAttribute("user", user);
            System.out.println(user);
            logger.info("aaaaaasdasdasdasd");
            return true;
        }
        return false;
    }

    /*查看所有用户*/
    @GetMapping("/user_info/list_normal")
    public List<User> list_normal(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "10") int pageSize) {
        return service.selectAllUser(pageNum, pageSize);
    }

    @ApiOperation("根据用户名进行搜索")
    @GetMapping("/searchUser")
    public List<User> searchUser(String uname) {
        return service.searchUser(uname);
    }

    /* *//*查看被舉報用戶*//*
    @GetMapping("/reportUser")
    public List reportUser(@RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "10") int pageSize) {
        List<User> list = service.selectReportUser(pageNum, pageSize);
        return list;
    }*/

    /*查看封禁用户*/
    @GetMapping("user_info/list_ban")
    public List list_ban(@RequestParam(defaultValue = "1") int pageNum,
                         @RequestParam(defaultValue = "10") int pageSize) {
        List<User> list = service.selectBanUser(pageNum, pageSize);
        return list;
    }

    /*刪除用戶(封禁)*/
    @GetMapping("rmUser")
    public void rmUser(int uid) {
        service.rmUserByUid(uid);
    }

    @ApiOperation("解封用户")
    @GetMapping("/Unsealing")
    public void Unsealing(int uid) {
        service.unSealing(uid);
    }


}
